package com.qqhf.hitfood.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.qqhf.common.config.AppConfiguration;
import com.qqhf.common.entity.Constants;
import com.qqhf.common.utils.DateUtil;
import com.qqhf.hitfood.service.IOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class OssServiceImpl implements IOssService {
    @Autowired
    private AppConfiguration appConfiguration;

    @Override
    public String uploadFile(MultipartFile file, String type, String name) {
        String endpoint = appConfiguration.getOssEndpoint();
        String accessKeyId = appConfiguration.getOssKeyid();
        String accessKeySecret = appConfiguration.getOssKeysecret();
        String bucketName = appConfiguration.getOssBucketname();
        String folderName = ""; //文件路径
        switch (type) {
            case "admin":   //管理员头像，传入的是管理员名称
                folderName = Constants.ADMIN_FOLDER;
                break;
            case "waiter":  //店管理员头像，传入的是店管理员名称
                folderName = Constants.WAITER_FOLDER;
                break;
            case "user":    //用户头像，传入的是用户名
                folderName = Constants.USER_FOLDER;
                break;
            case "shop":    //店铺头像，传入的是店铺名
                folderName = Constants.SHOP_FOLDER;
                break;
            default:        //食品图片，传入的是：店铺名
                folderName = Constants.FOOD_FOLDER;
                break;
        }
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 获取文件后缀
            String filename = file.getOriginalFilename();
            String prefix = filename.substring(filename.lastIndexOf("."));
            //文件路径+文件名
            String objectName = folderName + DateUtil.getAllTime()+ prefix;

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file.getInputStream());
            // 上传文件。
            ossClient.putObject(putObjectRequest);
            //hitfood.oss-cn-beijing.aliyuncs.com => https://qqhf.ltd
            String url = "https://hitfood.oss-cn-beijing.aliyuncs.com/" + objectName;
            return url;
        } catch (OSSException oe) {
            System.out.println("OSS服务出错。" + oe.getMessage());
            return null;
        } catch (ClientException ce) {
            System.out.println("客户端访问OSS出错。" + ce.getMessage());
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
